const express = require('express');
const { createObject, getObjects, getObjectById, updateObject, deleteObject } = require('../controllers/objectController');
const { body, validationResult } = require('express-validator');
const router = express.Router();

/**
 * @swagger
 * components:
 *   schemas:
 *     Object:
 *       type: object
 *       required:
 *         - jsonData
 *       properties:
 *         _id:
 *           type: string
 *           description: 对象ID
 *           readOnly: true
 *         jsonData:
 *           type: object
 *           description: JSON数据
 */

/**
 * @swagger
 * /object/create:
 *   post:
 *     summary: 创建一个新对象
 *     tags: [Objects]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/Object'
 *     responses:
 *       201:
 *         description: 对象创建成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Object'
 *       500:
 *         description: 服务器错误
 */
router.post(
  '/create',
  [
    body('jsonData').isObject().withMessage('jsonData 必须是一个对象')
  ],
  (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    next();
  },
  createObject
);

/**
 * @swagger
 * /object/list:
 *   get:
 *     summary: 获取分页的对象列表
 *     tags: [Objects]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         required: false
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           default: 10
 *         required: false
 *         description: 每页的对象数
 *     responses:
 *       200:
 *         description: 返回分页的对象列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 objects:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Object'
 *                 pageInfo:
 *                   type: object
 *                   properties:
 *                     currentPage:
 *                       type: integer
 *                     pageSize:
 *                       type: integer
 *                     totalObjects:
 *                       type: integer
 *                     totalPages:
 *                       type: integer
 *       500:
 *         description: 服务器内部错误
 */
router.get('/list', getObjects);

/**
 * @swagger
 * /object/get:
 *   get:
 *     summary: 根据ID获取对象
 *     tags: [Objects]
 *     parameters:
 *       - in: query
 *         name: id
 *         schema:
 *           type: string
 *         required: true
 *         description: 对象ID
 *     responses:
 *       200:
 *         description: 根据ID返回对象描述
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Object'
 *       404:
 *         description: 对象未找到
 */
router.get('/get', getObjectById);

/**
 * @swagger
 * /object/update:
 *   put:
 *     summary: 根据ID更新对象
 *     tags: [Objects]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: string
 *                 description: 要更新的对象的ID
 *                 example: "60d5f44f4e7a4c3a80f8d1e4"
 *               jsonData:
 *                 type: object
 *                 description: 要更新的JSON数据
 *     responses:
 *       200:
 *         description: 对象已更新
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 对象已更新
 *       400:
 *         description: 无效的请求
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 无效的 ObjectId
 *       404:
 *         description: 对象未找到
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 对象未找到
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 服务器内部错误
 */
router.put(
  '/update',
  [
    body('jsonData').isObject().withMessage('jsonData 必须是一个对象')
  ],
  (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    next();
  },
  updateObject
);

/**
 * @swagger
 * /object/delete:
 *   delete:
 *     summary: 根据ID删除对象
 *     tags: [Objects]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: string
 *                 description: 要删除的对象的ID
 *                 example: "60d5f44f4e7a4c3a80f8d1e4"
 *     responses:
 *       200:
 *         description: 对象已删除
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 对象已删除
 *       400:
 *         description: 无效的请求
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 无效的 ObjectId
 *       404:
 *         description: 对象未找到
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 对象未找到
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 服务器内部错误
 */
router.delete(
  '/delete',
  (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    next();
  },
  deleteObject
);

module.exports = router;
